<template>
    <div>
        <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()">
            <Icon type="ios-arrow-back"/>
            返回
        </Button>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-main">
                    <Form ref="formCustom" :model="formCustom" :rules="ruleCustom" :label-width="80">
                        <Row>
                            <Col span="7">
                                <Form-item label="项目名称" prop="name">
                                    <i-input type="text" v-model="formCustom.name" placeholder="请输入项目名称"></i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="项目专班" prop="special">
                                    <i-input type="text" v-model="formCustom.special" placeholder="请输入项目专班"></i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="开工时间" prop="start_date">
                                    <DatePicker @on-change="startChange" :value="formCustom.start_date"
                                                format="yyyy-MM-dd"
                                                type="date" placeholder="选择开工时间"></DatePicker>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="项目进度" prop="progress">
                                    <i-input type="number" number v-model="formCustom.progress" placeholder="请输入项目进度">
                                        <span slot="append">%</span>
                                    </i-input>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="进度状况" prop="progress_status">
                                    <Select v-model="formCustom.progress_status" style="width:100%"
                                            placeholder="选择进度状况">
                                        <Option value="0">顺利推进</Option>
                                        <Option value="1">推进迟缓</Option>
                                    </Select>
                                </Form-item>
                            </Col>
                            <Col span="7">
                                <Form-item label="上报日期" prop="imp_date">
                                    <DatePicker @on-change="dateChange" :options="options1"  :value="formCustom.imp_date" format="yyyy-MM-dd"
                                                type="date" placeholder="选择上报日期"></DatePicker>
                                </Form-item>
                            </Col>
                        </Row>
                        <Form-item label="存在问题" prop="remarks">
                            <i-input type="textarea"  v-model="formCustom.remarks"
                                     placeholder="请输入项目问题"></i-input>
                        </Form-item>
                    </Form>
                    <div class="line-button" v-if="!showDetail">
                        <Button type="primary" @click="validateForm('formCustom')">保存</Button>
                    </div>
                </div>
            </div>
        </Card>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="handleSubmit"
        >
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>

    </div>
</template>

<script>
import { insert, getById, update } from '@/api/outbreak-management/resume-work/project-info'

export default {
  data () {
    const validateInt = (rule, value, callback) => {
      if (value % 1 !== 0) {
        callback(new Error('请输入整数'))
      } else {
        callback()
      }
    }
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 弹窗控制
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      formCustom: {
        name: '',
        special: '',
        start_date: '',
        progress: '',
        progress_status: '',
        remarks: '',
        imp_date: ''
      },
      ruleCustom: {
        name: [
          { required: true, message: '项目名称必填', trigger: 'blur' },
          { min: 0, max: 12, message: '项目名称长度不能超过12', trigger: 'blur' }
        ],
        special: [
          { required: true, message: '项目专班必填', trigger: 'blur' },
          { min: 0, max: 12, message: '项目专班长度不能超过12', trigger: 'blur' }
        ],
        start_date: [
          { required: true, message: '项目开工日期必选', trigger: 'blur' }
        ],
        progress: [
          { required: true, type: 'number', message: '项目进度必填', trigger: 'blur' },
          { min: 0, max: 100, type: 'number', message: '项目进度范围0-100', trigger: 'blur' },
          { validator: validateInt, trigger: 'blur' }
        ],
        progress_status: [
          { required: true, message: '项目状况必选', trigger: 'blur' }
        ],
        remarks: [
          { min: 0, max: 12, message: '项目问题长度不能超过12', trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报日期必选', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },

    /**
       * 日期格式化
       */
    dateChange (date) {
      this.formCustom.imp_date = date
    },
    /**
       * 日期格式化
       */
    startChange (date) {
      this.formCustom.start_date = date
    },
    handleSubmit () {
      let requestData = this.formCustom
      if (requestData.id) {
        update(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.id) {
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.id)
      }
    },
    getEditData (id) {
      getById({ id }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.formCustom = res
          this.formCustom.imp_date = this.$common.moment(res.imp_date).format('YYYY-MM-DD')
          this.formCustom.start_date = this.$common.moment(res.start_date).format('YYYY-MM-DD')
          this.formCustom.progress = parseInt(res.progress)
        }
        console.log(data)
      })
    }
  },
  created () {
    this.isEditStatus()
  },
  mounted () {
  }
}
</script>

<style scoped>

</style>
